#!/usr/bin/env python
# coding=utf-8
"""
@file: log_util.py
@contact: bianzhiwei@iyoujia.com
@time   : 2019/4/1 21:04
@Desc   :
"""
import os
import sys
import logging
from logging import handlers

# 当前文件的路径
pwd, filename = os.path.split(os.path.abspath(__file__))
# 当前文件的父路径
father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")
# 当前文件的前两级目录
grader_father = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")


def get_app_name():
    return os.path.splitext(os.path.basename(sys.argv[0]))[0]


# def init_logging2(log_file_path=None, file_name=None):
#     file_name = file_name if file_name else development_util.get_app_name()
#     path = log_file_path
#     if not os.path.exists(path):
#         os.makedirs(path)
#     file_name = path + file_name + '.log'
#     print(file_name)
#     log_format = '%(asctime)s.%(msecs)03d %(thread)d %(levelname)s %(module)s %(lineno)d - %(funcName)s : %(message)s'
#     logging.basicConfig(
#         handlers=[
#             RotatingFileHandler(file_name, mode='a', maxBytes=300 * 1024 * 1024,
#                                 backupCount=2, encoding=None, delay=0),
#             logging.StreamHandler()
#         ],
#         level=logging.INFO,
#         format=log_format,
#         datefmt="%Y-%m-%d %H:%M:%S")


class Logger(object):
    level_relations = {
        'debug': logging.DEBUG,
        'info': logging.INFO,
        'warning': logging.WARNING,
        'error': logging.ERROR,
        'crit': logging.CRITICAL
    }  # 日志级别关系映射

    def __init__(self, log_file_path=None, level='info'):
        # log_format = '%(asctime)s.%(msecs)03d %(levelname)s %(module)s %(lineno)d - %(funcName)s : %(message)s'
        app_name = get_app_name()
        log_path = father_path + '/log/'
        if not os.path.exists(log_path):
            os.makedirs(log_path)
        log_file_path = log_file_path if log_file_path else log_path + app_name + '.log'
        # self.logger = logging.getLogger(log_file_path)
        # format_str = logging.Formatter(log_format)  # 设置日志格式
        # self.logger.setLevel(self.level_relations.get(level))  # 设置日志级别
        # # sh = logging.StreamHandler()  # 往屏幕上输出
        # # sh.setFormatter(format_str)  # 设置屏幕上显示的格式
        #
        # th = handlers.RotatingFileHandler(log_file_path, mode='a', maxBytes=300 * 1024 * 1024,
        #                                   backupCount=2, encoding=None, delay=0)  # 往文件里写入#指定间隔时间自动生成文件的处理器
        # th.setFormatter(format_str)  # 设置文件里写入的格式
        # # self.logger.addHandler(sh)  # 把对象加到logger里
        # self.logger.addHandler(th)
        logging.basicConfig(
            handlers=[
                handlers.RotatingFileHandler(log_file_path, mode='a', maxBytes=300 * 1024 * 1024,
                                             backupCount=2, encoding=None, delay=0),
                logging.StreamHandler()
            ],
            level=logging.INFO,
            format=
            '%(asctime)s.%(msecs)03d %(thread)d %(levelname)s %(module)s %(lineno)d - %(funcName)s : %(message)s',
            datefmt="%Y-%m-%d %H:%M:%S")


# def process():
#     log = Logger().logger
#     log.debug('debug')
#     log.info('info')
#     log.warning('警告')
#     log.error('报错')
#     log.critical('严重')

Logger()
# log = Logger().logger
log = logging
# if __name__ == '__main__':
# process()
